package yxy.game.pm2.module.temple;

import yxy.cherry.battle.module.Camp;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.heroadvanceddata;
import yxy.cherry.data.bean.herodata;
import yxy.cherry.data.bean.heroadvanced;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.module.hero.HeroHandle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class TempleHandle {

    /**
     * 七星祭坛武将筛选
     */
    public Collection<HeroMessage.HeroSimple> sevenStarHeros(String playerCode) {
        HeroHandle heroHandle = new HeroHandle();
        ArrayList<HeroMessage.HeroSimple> list = new ArrayList<>();
        for (Hero hero : heroHandle.getFreeHeros(playerCode).values()) {
            heroadvanced _heroadvanced = DataCenter.getData(hero.advancedCode(), heroadvanced.class);
            if (_heroadvanced == null) {
                throw new NullPointerException("playerCode:" + playerCode + "------hero.advancedCode():" + hero.advancedCode() + "------hero.getStage():" + hero.getStage());
            }
            if (_heroadvanced.getBackoff() == 1) {
                list.add(heroHandle.getHeroSimple(playerCode, hero));
            }
        }
        return list;
    }

    /**
     * 置换神殿武将筛选
     */
    public Collection<HeroMessage.HeroSimple> templeHeros(String playerCode) {
        HeroHandle heroHandle = new HeroHandle();
        ArrayList<HeroMessage.HeroSimple> list = new ArrayList<>();
        for (Hero hero : heroHandle.getHeros(playerCode)) {
            heroadvanced _heroadvanced = DataCenter.getData(hero.advancedCode(), heroadvanced.class);
            if (_heroadvanced.getBarter() == 1) {
                list.add(heroHandle.getHeroSimple(playerCode, hero));
            }
        }
        return list;
    }

    /**
     * 置换神殿目标武将筛选
     */
    public Collection<HeroMessage.HeroSimple> templeTargetHeros(String playerCode, Hero hero) {
        HeroHandle heroHandle = new HeroHandle();
        ArrayList<HeroMessage.HeroSimple> list = new ArrayList<>();

        herodata _herodata = DataCenter.getData(hero.getDataId(), herodata.class);
        Camp camp = Camp.forNumber(_herodata.getPower());

        for (Hero targetHero : heroHandle.getFreeHeros(playerCode).values()) {
            if (targetHero.getCode().equals(hero.getCode()) || targetHero.getDataId() / 100 == hero.getDataId() / 100) {
                continue;
            }
            heroadvanced _heroadvanced = DataCenter.getData(targetHero.advancedCode(), heroadvanced.class);

            if (_heroadvanced.getPassbarter() == 1) {
                _herodata = DataCenter.getData(targetHero.getDataId(), herodata.class);
                Camp targetCamp = Camp.forNumber(_herodata.getPower());
                switch (camp) {
                    case CAMP_吴:
                    case CAMP_蜀:
                    case CAMP_魏:
                    case CAMP_群:
                        switch (targetCamp) {
                            case CAMP_神:
                            case CAMP_魔:
                                continue;
                        }
                        break;
                    case CAMP_神:
                    case CAMP_魔:
                        switch (targetCamp) {
                            case CAMP_吴:
                            case CAMP_蜀:
                            case CAMP_魏:
                            case CAMP_群:
                                continue;
                        }
                        break;
                }
                list.add(heroHandle.getHeroSimple(playerCode, targetHero));
            }
        }
        return list;
    }

    /**
     * 高级祭坛武将筛选
     */
    public Collection<HeroMessage.HeroSimple> seniorAltarHeros(String playerCode) {
        HeroHandle heroHandle = new HeroHandle();
        ArrayList<HeroMessage.HeroSimple> list = new ArrayList<>();
        for (Hero hero : heroHandle.getHeros(playerCode)) {
            heroadvanced _heroadvanced = DataCenter.getData(hero.advancedCode(), heroadvanced.class);
            if (_heroadvanced.getSeniorbackoff() == 1) {
                list.add(heroHandle.getHeroSimple(playerCode, hero));
            }
        }
        return list;
    }


    /**
     * 置换神殿武将置换所需要的数量返回
     */
    public static  HashMap<String, Integer> getNum(Hero hero) {
        HashMap<String, Integer> map = new HashMap<>();
        int dataId = hero.getDataId();//武将id
        int heroStar = hero.getStar();//当前星级
        int num = 0;                  //消耗的本体数量
        HashMap<Integer, Integer> map_heroadvanceddata = new HashMap<>();//key武将星级，value武将id
        //todo 获取该武将所有id集合
        heroadvanceddata data = DataCenter.getData(dataId, heroadvanceddata.class);
        while (data != null) {
            int id = data.getId();
            int startop = data.getStartop();
            map_heroadvanceddata.put(startop, id);
            data = DataCenter.getData(data.getDegenerationid(), heroadvanceddata.class);
        }

        Integer hero_fiveStarId = map_heroadvanceddata.get(5);

        //todo 计算该武将星级所需要的本体数量
        for (Map.Entry<Integer, Integer> entry : map_heroadvanceddata.entrySet()) {
            Integer heroId = entry.getValue();
            for (int star = 5; star < heroStar; star++) {
                heroadvanced _heroadvanced = DataCenter.getData(String.format("%d_%d", heroId, star), heroadvanced.class);
                if (_heroadvanced == null) continue;
                //获取升星的本体消耗
                int type = _heroadvanced.getConditiontype()[0];
                if (type == 1) {
                    num += _heroadvanced.getCost3()[0];
                }

            }
        }

        //todo 还要记上五星的本体
        map.put("num",num + 1);
        map.put("dataId",hero_fiveStarId);
        return map;
    }
}
